﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyMvc.Web.Common;
using EasyMvc.Models;
using EasyMvc.Controls;
using EasyMvc.Core;
using EasyMvc.Utility;

namespace EasyMvc.Web.Areas.MWeb.Controllers
{
    [Permission(EnumViewPermission.ManageRole)]
    public class RoleController : MWebControllerBase
    {
        #region Actions
        public ActionResult Index(Role model, int id = 1)
        {    
            IQueryable<Role> queryList = this.DbContext.Roles;

            if (!string.IsNullOrEmpty(model.Name))
                queryList = queryList.Where(u => u.Name.Contains(model.Name));

            var result = queryList.OrderByDescending(u => u.Id).ToPagedList(id, this.PageSize);

            return View(result);
        }

        //
        // GET: /Home/Edit/5

        public ActionResult Edit(int id)
        {
            var model = this.DbContext.Roles.Find(id);

            RenderViewData(model);

            return View(model);
        }

        //
        // POST: /Home/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            if (!ModelState.IsValid)
                return View();

            var model = this.DbContext.Roles.Find(id);
            this.TryUpdateModel<Role>(model);

            var viewPermissions = Fetch.GetIds("ViewPermission").Select(p => (EnumViewPermission)p).ToList();
            var permission = new Permission(){ViewPermissions = viewPermissions};
            model.Permission = PermissionHelper.ToJson(permission);

            this.DbContext.SaveChanges();

            return this.RefreshParent();
        }

        //
        // GET: /Home/Create

        public ActionResult Create()
        {
            var model = new Role();
            RenderViewData(model);

            return View("Edit", model);
        }

        //
        // POST: /Home/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            var model = new Role();
            this.TryUpdateModel<Role>(model);

            var viewPermissions = Fetch.GetIds("ViewPermission").Select(p => (EnumViewPermission)p).ToList();
            var permission = new Permission() { ViewPermissions = viewPermissions };
            model.Permission = PermissionHelper.ToJson(permission);

            this.DbContext.Roles.Add(model);
            this.DbContext.SaveChanges();

            return this.RefreshParent();
        }

        [HttpPost]
        public ActionResult Delete(FormCollection collection)
        {
            var ids = Fetch.GetIds("ids");
            ids.ToList().ForEach(id => this.DbContext.Roles.Remove(this.DbContext.Roles.Find(id)));
            this.DbContext.SaveChanges();

            return View("Index");
        }

        private void RenderViewData(Role model)
        {
            var roleViewPermissions = PermissionHelper.Parse(model.Permission);

            var allViewPermissions = EnumHelper.GetItemValueList<EnumViewPermission>();
            this.ViewData.Add("ViewPermission", new SelectList(allViewPermissions, "Key", "Value", string.Join(",", roleViewPermissions.ViewPermissions.Select(p => (int)p))));
        }
        #endregion
    }
}
